<template>
  <div class="login-container">
    <el-form ref="form" :rules="rules" :model="form" label-width="80px" class="reset-form">
        <h2 class="login-title">重置密码</h2>
        <el-form-item label="邮箱" prop="username">
          <el-input v-model="form.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="旧密码" prop="passwordold">
          <el-input v-model="form.passwordold" show-password></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="password">
          <el-input v-model="form.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="password2">
          <el-input v-model="form.password2" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('form')">确定</el-button>
          <el-button type="primary" @click="cancel()">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
</template>
<script>
import { RSAEncrypt } from '@/api/password/password'
import { resetPasswordAfterLogin } from '@/api/request/user'
// import { Message } from 'element-ui'
// import store from '@/store/store'
export default {
  data () {
    return {
      form: {
        username: '',
        password: '',
        password2: '',
        passwordold: ''
      },
      rules: {
        username: [
          {required: true, message: '用户名不能为空', trigger: 'blur'},
          {min: 3, max: 50, message: '用户名3-50位', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '密码不能为空', trigger: 'blur'},
          {min: 3, max: 15, message: '密码3-15位', trigger: 'blur'}
        ],
        password2: [
          {required: true,
            trigger: 'blur',
            validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('密码不能为空'))
              } else if (value !== this.form.password) {
                callback(new Error('密码不一致'))
              } else {
                this.form.password = value
                callback()
              }
            }},
          {min: 3, max: 15, message: '密码3-15位', trigger: 'blur'}
        ],
        passwordold: [
          {required: true, message: '密码不能为空', trigger: 'blur'},
          {min: 3, max: 15, message: '密码3-15位', trigger: 'blur'}
        ]
      }
    }
  },
  activated () {
    this.form.username = this.$store.getters.user
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(valid => {
        // console.log(valid) 验证通过为true，有一个不通过就是false
        if (valid) {
          const enpas = RSAEncrypt(this.form.password)
          const enpasold = RSAEncrypt(this.form.passwordold)
          console.log(enpas)
          resetPasswordAfterLogin({
            email: this.form.username,
            password_new: enpas,
            password_old: enpasold
          }).then(data => {
            console.log(data)
            this.$router.go(-1)
          })
        } else {
          console.log('验证失败')
          return false
        }
      })
    },
    cancel () {
      this.$router.go(-1)
    }
  }
}
</script>

<style acoped>
  .reset-form {
    width: 400px;
    margin: 160px auto; /* 上下间距160px，左右自动居中*/
    background-color: rgb(255, 255, 255, 0.8); /* 透明背景色 */
    padding: 30px;
    border-radius: 20px; /* 圆角 */
  }

  /* 背景 */
  .login-container {
    position: absolute;
    width: 100%;
    height: 100%;
    background-color: rgba(105, 148, 212, 0.8);
  }

  /* 标题 */
  .login-title {
    color: #f39810;
    text-align: center;
  }

</style>
